package com.mdoya.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mdoya.domain.Category;
import com.mdoya.query.CategoryQuery;
import com.mdoya.response.ResultResponse;
import com.mdoya.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @author mdoya
 * @version 1.0
 * @description:
 */
@Slf4j
@RequestMapping("/category")
@RestController
public class CategoryController {

    private CategoryService categoryService;

    public CategoryController(CategoryService c){
        this.categoryService = c;
    }

    @PostMapping
    public ResultResponse insert(@RequestBody Category category){
        log.info("新增菜品和套餐分类: {}",category);
        ResultResponse resultResponse = new ResultResponse<>();
        Integer rows = categoryService.insert(category);
        if (rows > 0){
            resultResponse.setCode(1);
            resultResponse.setData("新增分类成功");
            return resultResponse;
        }
        resultResponse.setCode(-1);
        resultResponse.setData("新增分类失败");
        return resultResponse;
    }
    @DeleteMapping
    public ResultResponse deleteCategoryId(Long id,Integer type){
        log.info("删除菜品和套餐分类: {}",id,"  :  ",type);
        ResultResponse resultResponse = new ResultResponse<>();
        Integer rows = categoryService.deleteById(id, type);
        if (rows > 0){
            resultResponse.setCode(1);
            resultResponse.setData("删除分类成功");
            return resultResponse;
        }
        resultResponse.setCode(-1);
        resultResponse.setData("删除分类失败");
        return resultResponse;
    }
    @PutMapping
    public ResultResponse updateById(@RequestBody Category category){
        log.info("更新菜品和套餐分类: {}",category);
        ResultResponse resultResponse = new ResultResponse<>();
        Integer rows = categoryService.updateById(category);
        if (rows > 0){
            resultResponse.setCode(1);
            resultResponse.setData("更新分类成功");
            return resultResponse;
        }
        resultResponse.setCode(-1);
        resultResponse.setData("更新分类失败");
        return resultResponse;
    }
    @GetMapping("/page")
    public ResultResponse selectPage(CategoryQuery categoryQuery){
        log.info("查询所有分类: {}",categoryQuery);
        ResultResponse resultResponse = new ResultResponse<>();
        resultResponse.setCode(1);
        resultResponse.setData(categoryService.selectPage(categoryQuery));
        return resultResponse;
    }

    /**
     *  根据type类型查询数据
     * @param type
     * @return
     */
    @GetMapping("/list")
    public ResultResponse selectTypeCategoryList(Integer type){
        log.info("type类型: {}",type);
        ResultResponse resultResponse = new ResultResponse<>();
        resultResponse.setCode(1);
        resultResponse.setData(categoryService.selectTypeCategoryList(type));
        return resultResponse;
    }

    /**
     *    首页查询所有分类类型
     * @return
     */
   /* @GetMapping("/findTypeAll")
    public ResultResponse findTypeAll(){
        log.info("首页查询所有类型");
        return ResultResponse.success(categoryService.findTypeAll());
    }*/
}
